package com.itheima.test.Demo8;

import java.io.*;

/*
对象序列化ObjectOutputStream
想要用序列化必须让类实现Serializable
反序列化ObjectInputStream

 */
public class Demo8 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //创建一个写入指定的OutputStream的ObjectOutputStream
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("day13_字符流\\d.txt"));
        //创建学生对象并传为信息
        Student stu1 = new Student("杨幂",18);
        Student stu2 = new Student("柳岩",18);
        //写入当中
        oos.writeObject(stu1);
        oos.writeObject(stu2);
        //关闭资源
        oos.close();


        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("day13_字符流\\d.txt"));
        Student s = (Student)ois.readObject();
        System.out.println(s.getName()+"..."+s.getAge());
        Student ss = (Student)ois.readObject();
        System.out.println(ss.getName()+"..."+ss.getAge());

    }
}
